package com.atguigu.gmall.product.controller;

import brave.http.HttpServerRequest;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseSaleAttr;
import com.atguigu.gmall.model.product.SpuImage;
import com.atguigu.gmall.model.product.SpuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.service.ManageService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/admin/product")
public class SpuManageController {


    @Autowired
    private ManageService manageService;

    //  /admin/product/{page}/{limit}
    //  http://localhost/admin/product/1/10?category3Id=13
    //  springMvc : 对象传值 {当对象中的属性名，与传递的参数名称一致时，会自动封装到对象中！}
    /*
        <from action="login" method="post" >
            <input type ="text" name="username" >
            <input type ="password" name="pwd" >
            <input type ="submit" name="提交" >
        </form>

       class User{
            private int id;
            private String username;
            private String pwd;

       }

       @PostMapping("login")
       public void login(User user ){
        //  调用服务层方法
        String userName =  request.getParameter("username");
        String pwd = request.getParameter("pwd");

        userService.login(userName,pwd);
        userService.login(user);
       }

     */
    @GetMapping("{page}/{limit}")
    public Result getSpuInfoList(@PathVariable Long page,
                                 @PathVariable Long limit,
                                 SpuInfo spuInfo ){

        //  select * from spu_info where category3_id = 61 limit 1, 1;
        //  mybatis 分页插件：
        Page pageModel = new Page(page,limit);

        IPage<SpuInfo> iPage = this.manageService.getSpuInfoList(pageModel,spuInfo);

        return Result.ok(iPage);
    }

    //  /admin/product/baseSaleAttrList
    //  获取所有的销售属性数据：
    @GetMapping("baseSaleAttrList")
    public Result getSaleAttrList(){
        //  调用服务层方法：
        List<BaseSaleAttr> baseSaleAttrList = this.manageService.getSaleAttrList();
        return Result.ok(baseSaleAttrList);
    }

    //  /admin/product/saveSpuInfo
    //  Json ---> JavaObject
    @PostMapping("saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo){
        //  调用保存方法
        this.manageService.saveSpuInfo(spuInfo);
        return Result.ok();
    }

    //  /admin/product/spuImageList/{spuId}
    @GetMapping("spuImageList/{spuId}")
    public Result getSpuImageList(@PathVariable Long spuId){
        //  调用服务层方法：
        List<SpuImage> spuImageList = this.manageService.getSpuImageList(spuId);
        return Result.ok(spuImageList);
    }

    //  /admin/product/spuSaleAttrList/{spuId}
    @GetMapping("spuSaleAttrList/{spuId}")
    public Result getSpuSaleAttrList(@PathVariable Long spuId){
        //  返回的泛型：
        List<SpuSaleAttr> spuSaleAttrList = this.manageService.getSpuSaleAttrList(spuId);
        return Result.ok(spuSaleAttrList);
    }

    @GetMapping("getSpuInfo/{spuId}")
    public Result getSpuInfo(@PathVariable Long spuId) {
        return Result.ok(manageService.getFullSpuInfo(spuId));
    }

    @PostMapping("updateSpuInfo")
    public Result updateSpuInfo(@RequestBody SpuInfo spuInfo){
        // 调用服务层的保存方法
        manageService.updateSpuInfo(spuInfo);
        return Result.ok();
    }



}
